package com.ufop.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.Toolkit;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.JTable;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JInternalFrame;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextArea;

import com.toedter.calendar.JDateChooser;
import com.ufop.controller.PautaController;
import com.ufop.model.ItemPauta;
import com.ufop.model.Pauta;

import java.awt.Font;
import javax.swing.JScrollPane;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.ImageIcon;

public class PautaView extends JInternalFrame implements Observer, ActionListener {
	private PautaController pautaCtrl;
	private Pauta pautaModel;	

	private JPanel contentPane;
	private JTextField txtID;
	private JDateChooser cldData;
	private DefaultTableModel dtmPauta;
	private DefaultTableModel dtmIPauta;
	private JTable tableIPauta;
	private JTable tablePauta;
	private JButton btnNovaPauta;
	private JButton btnSalvarPauta;
	private JButton btnExcluirPauta;
	private JTextArea txtObserv;
	private JTextField txtExpediente;
	private JButton btnAddItem;
	private JButton btnRemoveItem;
	private JButton btnTurno;

	/**
	 * Create the frame.
	 */
	public PautaView(PautaController pautaCtrl, Pauta pautaModel){
		setClosable(true);
		this.pautaCtrl = pautaCtrl;
		pautaModel.addObserver(this);
		this.pautaModel = pautaModel;
		criarComponentes();
		//		atualizaPauta();

	}

	private void atualizaPauta(){
		limpaTabelaPauta();
		for(Pauta p : pautaModel.getListPautas())
			dtmPauta.addRow(new Object[]{p.getIdPauta(), p.getDataPauta()});	
	}

	private void limpaTabelaPauta(){
		while(0 < dtmPauta.getRowCount())
			dtmPauta.removeRow(0);
	}

	private void atualizaItemPauta(){
		limpaTabelaIPauta();
		if(pautaModel.getItemPauta().size() != 0){
			int cont = 1;
			for(ItemPauta iP : pautaModel.getItemPauta())
				dtmIPauta.addRow(new Object[]{cont, iP.getExpediente(), iP.getTurno().size()});	
		}
		preencheCamposPauta();
	}

	private void preencheCamposPauta(){
		try{
			if(tablePauta.getSelectedRow() != -1){
				txtID.setText(pautaModel.getIdPauta() + "");
				cldData.setDate(pautaModel.getDataPauta());
				txtObserv.setText(pautaModel.getObsPauta());
			}
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
	}

	private void limpaTabelaIPauta(){
		while(0 < dtmIPauta.getRowCount())
			dtmIPauta.removeRow(0);
	}

	@Override
	public void update(Observable o, Object arg) {
		if(arg.equals("Pauta"))
			atualizaPauta();
		else if(arg.equals("IPauta"))
			atualizaItemPauta();


	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String opcao = e.getActionCommand();
		if(opcao.equals("NovaPauta")){
			//Remove selecao tabela
			if(dtmPauta.getRowCount() != 0)
				tablePauta.removeRowSelectionInterval(0, dtmPauta.getRowCount()-1);
		}else if(opcao.equals("AddItem")){
			pautaCtrl.AddItemPauta(txtExpediente.getText());
			txtExpediente.setText("");
		}else if(opcao.equals("ExcluiItem")){
			if(tableIPauta.getSelectedRow() != -1)
				pautaCtrl.ExcluirItemPauta(tableIPauta.getSelectedRow());
		}
	}	

	private void criarComponentes(){
		setTitle("Pauta");
		setFrameIcon(new ImageIcon(PautaView.class.getResource("/Icones/pauta_24x24.png")));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 674, 362);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Descri\u00E7\u00E3o", TitledBorder.LEADING, TitledBorder.TOP, null, null));

		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Item Pauta", TitledBorder.LEADING, TitledBorder.TOP, null, null));

		JTableHeader header = new JTableHeader();
		header.setBackground(Color.GRAY);
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addComponent(panel_1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 644, Short.MAX_VALUE)
						.addComponent(panel, GroupLayout.DEFAULT_SIZE, 644, Short.MAX_VALUE))
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 128, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 186, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(32, Short.MAX_VALUE))
		);

		//TABELA DE ITEMS PAUTA		
		dtmIPauta = new DefaultTableModel(new Object[][]{}, new String[]{"idItem", "Expediente", "Qtde Turnos"});
		tableIPauta = new JTable(dtmIPauta);
		//				tableCompras.setShowVerticalLines(false);
		//				tableCompras.setShowHorizontalLines(false);

		//tornar coluna INVISIVEL
		tableIPauta.getColumnModel().getColumn(0).setPreferredWidth(200);
		tableIPauta.getColumnModel().getColumn(1).setPreferredWidth(200);
		tableIPauta.getColumnModel().getColumn(2).setPreferredWidth(200);
		ListSelectionModel rows = tableIPauta.getSelectionModel();
		rows.addListSelectionListener(new ListSelectionListener() {

			@Override
			public void valueChanged(ListSelectionEvent e) {
				pautaModel.setIdItemPauta(tableIPauta.getSelectedRow());
			}
		});		
		
		header = tableIPauta.getTableHeader();
		header.setBackground(Color.GRAY);

		JScrollPane scrollPaneItemPauta = new JScrollPane(tableIPauta);
		scrollPaneItemPauta.setBorder(null);

		btnRemoveItem = new JButton("");
		btnRemoveItem.setActionCommand("ExcluiItem");
		btnRemoveItem.addActionListener(this);	
		btnRemoveItem.setIcon(new ImageIcon(PautaView.class.getResource("/Icones/deletar_13x13.png")));

		btnTurno = new JButton("Turno");
		btnTurno.setActionCommand("TurnoView");
		btnTurno.addActionListener(pautaCtrl);	

		JButton btnVotantes = new JButton("Votantes");
		btnVotantes.setEnabled(false);
		
		txtExpediente = new JTextField();
		txtExpediente.setColumns(10);
		
		JLabel lblExpediente = new JLabel("Expediente:");
		
		btnAddItem = new JButton("");
		btnAddItem.setActionCommand("AddItem");
		btnAddItem.addActionListener(this);		
		btnAddItem.setIcon(new ImageIcon(PautaView.class.getResource("/Icones/add_13x13.png")));
		
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addGroup(Alignment.TRAILING, gl_panel_1.createSequentialGroup()
							.addContainerGap()
							.addComponent(scrollPaneItemPauta, GroupLayout.DEFAULT_SIZE, 527, Short.MAX_VALUE))
						.addGroup(gl_panel_1.createSequentialGroup()
							.addGap(13)
							.addComponent(lblExpediente)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(txtExpediente, GroupLayout.DEFAULT_SIZE, 462, Short.MAX_VALUE)))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addComponent(btnVotantes, Alignment.TRAILING)
						.addComponent(btnTurno, Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
						.addGroup(Alignment.TRAILING, gl_panel_1.createSequentialGroup()
							.addComponent(btnAddItem, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnRemoveItem, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap())
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_panel_1.createSequentialGroup()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
							.addComponent(lblExpediente)
							.addComponent(txtExpediente, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(btnAddItem, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
						.addComponent(btnRemoveItem))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addGroup(Alignment.TRAILING, gl_panel_1.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnTurno)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnVotantes))
						.addGroup(gl_panel_1.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(scrollPaneItemPauta, GroupLayout.PREFERRED_SIZE, 135, GroupLayout.PREFERRED_SIZE))))
		);
		panel_1.setLayout(gl_panel_1);

		JLabel lblID = new JLabel("ID:");

		txtID = new JTextField();
		txtID.setColumns(10);

		JLabel lblObs = new JLabel("Obs.:");

		txtObserv = new JTextArea();
		txtObserv.setLineWrap(true);
		txtObserv.setFont(new Font("Tahoma", Font.PLAIN, 11));
		txtObserv.setColumns(10);
		txtObserv.setBorder(UIManager.getBorder("TextField.border"));

		JLabel lblData = new JLabel("Data:");

		cldData = new JDateChooser();
		//		cldData.setColumns(10);

		btnSalvarPauta = new JButton(">>");
		btnSalvarPauta.setActionCommand("SalvarPauta");
		btnSalvarPauta.addActionListener(pautaCtrl);

		btnExcluirPauta = new JButton("<<");
		btnExcluirPauta.setActionCommand("ExcluirPauta");
		btnExcluirPauta.addActionListener(pautaCtrl);

		//TABELA PAUTA		
		dtmPauta = new DefaultTableModel(new Object[][]{}, new String[]{"idPauta", "Data Pauta"});
		tablePauta = new JTable(dtmPauta);
		//				tableCompras.setShowVerticalLines(false);
		//				tableCompras.setShowHorizontalLines(false);

		//tornar coluna INVISIVEL
		tablePauta.getColumnModel().getColumn(0).setPreferredWidth(200);
		tablePauta.getColumnModel().getColumn(1).setPreferredWidth(200);
		tablePauta.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		rows = tablePauta.getSelectionModel();
		rows.addListSelectionListener(new ListSelectionListener() {

			@Override
			public void valueChanged(ListSelectionEvent e) {
				pautaModel.carregaPauta(tablePauta.getSelectedRow());
				atualizaItemPauta();
			}
		});


		header = tablePauta.getTableHeader();
		header.setBackground(Color.GRAY);

		JScrollPane scrollPanePauta = new JScrollPane(tablePauta);
		scrollPanePauta.setBorder(null);

		btnNovaPauta = new JButton("");
		btnNovaPauta.setActionCommand("NovaPauta");
		btnNovaPauta.addActionListener(this);
		btnNovaPauta.setIcon(new ImageIcon(PautaView.class.getResource("/Icones/Novo_24x24.png")));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
				gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
						.addContainerGap()
						.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
								.addGroup(gl_panel.createSequentialGroup()
										.addComponent(lblID)
										.addGap(16)
										.addComponent(txtID, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(lblData)
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(cldData, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_panel.createSequentialGroup()
												.addComponent(lblObs)
												.addPreferredGap(ComponentPlacement.RELATED)
												.addComponent(txtObserv)))
												.addPreferredGap(ComponentPlacement.RELATED)
												.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
														.addComponent(btnNovaPauta, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
														.addComponent(btnSalvarPauta)
														.addComponent(btnExcluirPauta, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE))
														.addPreferredGap(ComponentPlacement.RELATED)
														.addComponent(scrollPanePauta, GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE)
														.addContainerGap())
				);
		gl_panel.setVerticalGroup(
				gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
						.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel.createSequentialGroup()
										.addComponent(btnNovaPauta)
										.addPreferredGap(ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
										.addComponent(btnSalvarPauta)
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(btnExcluirPauta)
										.addGap(5))
										.addComponent(scrollPanePauta, 0, 0, Short.MAX_VALUE)
										.addGroup(gl_panel.createSequentialGroup()
												.addContainerGap()
												.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
														.addComponent(lblID)
														.addComponent(txtID, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
														.addComponent(lblData)
														.addComponent(cldData, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
														.addPreferredGap(ComponentPlacement.RELATED)
														.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
																.addComponent(lblObs)
																.addComponent(txtObserv, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE))))
																.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
				);
		panel.setLayout(gl_panel);
		contentPane.setLayout(gl_contentPane);
	}


}
